home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / io-term.c < prev    next >
C/C++ Source or Header  |  1993-05-23  |  26KB  |  1,222 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ctype.h>
  20. #include <stdio.h>
  21.  
  22. #include "global.h"
  23.  
  24. #include "basic.h"
  25. #include "cell.h"
  26. #include "cmd.h"
  27. #include "format.h"
  28. #include "font.h"
  29. #include "getopt.h"
  30. #include "init.h"
  31. #define DEFINE_IO_VARS 1
  32. #include "io-abstract.h"
  33. #include "io-curses.h"
  34. #include "io-edit.h"
  35. #include "io-generic.h"
  36. #include "io-term.h"
  37. #include "io-utils.h"
  38. #include "io-x11.h"
  39. #include "key.h"
  40. #include "line.h"
  41. #include "lists.h"
  42. #define obstack_chunk_alloc ck_malloc
  43. #define obstack_chunk_free free
  44. #include "obstack.h"
  45. #include "oleofile.h"
  46. #include "print.h"
  47. #include "ref.h"
  48. #include "regions.h"
  49. #include "window.h"
  50. #include "funcs.h"
  51. #include "graph.h"
  52.  
  53. #ifdef USE_DLD
  54. /* If we're using dynamic linking, we get the names of the
  55.        functions to call by prepending the basename of save_name onto
  56.            _read_file
  57.            _write_file
  58.            _set_options
  59.            _show_options
  60.        so, if the file is sylk.o , the functions are named
  61.            sylk_read_file
  62.            sylk_write_file
  63.            sylk_set_options
  64.            sylk_show_options
  65.            */
  66. char *io_name;
  67. #else
  68. #include "list.h"
  69. #include "sc.h"
  70. #include "sylk.h"
  71. #include "panic.h"
  72. #endif
  73.  
  74.  
  75.  
  76.  
  77. /* This should be updated for every release.
  78.  * The file ANNOUNCE must be udpated as well.
  79.  */
  80. const char oleo_version_string[] = "Oleo version 1.4";
  81.  
  82. /* This variable is non-zero if the spreadsheet has been changed in any way */ 
  83. int modified = 0;
  84.  
  85. /* User settable options */
  86. int bkgrnd_recalc = 1;
  87. int auto_recalc = 1;
  88. int a0 = 0;
  89. int topclear = 0;
  90.  
  91. /* This is how frequently the alarm should go off. */
  92. unsigned int alarm_seconds = 1;
  93.  
  94. /* This is whether the alarm should go off at all. */
  95. unsigned int alarm_active = 1;
  96.  
  97. /* Jump here on error.  This simply restarts the top 
  98.  * level command loop.  User state should have been 
  99.  * reset appropriately before the longjmp.
  100.  */
  101. jmp_buf error_exception;
  102.  
  103. char * current_filename = 0;
  104.  
  105. /* These are the hooks used to do file-io. */
  106. #ifdef __STDC__
  107. void (*read_file) (FILE *, int) = oleo_read_file;
  108. void (*write_file) (FILE *, struct rng *) = oleo_write_file;
  109. int (*set_file_opts) (int, char *) = oleo_set_options;
  110. void (*show_file_opts) () = oleo_show_options;
  111. #else
  112. void (*read_file) () = oleo_read_file;
  113. void (*write_file) () = oleo_write_file;
  114. int (*set_file_opts) () = oleo_set_options;
  115. void (*show_file_opts) () = oleo_show_options;
  116. #endif
  117.  
  118. static char * disclaimer[] = 
  119. {
  120.   " Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation,Inc.\n",
  121.   "There is ABSOLUTELY NO WARRANTY for Oleo; see the file COPYING\n",
  122.   "for details.  Oleo is free software and you are welcome to distribute\n",
  123.   "copies of it under certain conditions; see the file COPYING to see the\n",
  124.   "conditions.\n\n",
  125.   0
  126. };
  127.  
  128. static char short_options[] = "Vqfxh";
  129. static struct option long_options[] =
  130. {
  131.   {"version", 0, NULL, 'V'},
  132.   {"quiet", 0, NULL, 'q'},
  133.   {"ignore-init-file", 0, NULL, 'f'},
  134.   {"nw", 0, NULL, 'x'},
  135.   {"help", 0, NULL, 'h'},
  136.   {NULL, 0, NULL, 0}
  137. };
  138.  
  139.  
  140. static char * usage[] = 
  141. {
  142.   " [--version] [--quiet] [--ignore-init-file] [--nw] [--help] \n",
  143.   " [-Vqfh] [file]\n",
  144.   0
  145. };
  146.  
  147. /* Avoid needless messages to stdout. */
  148. int spread_quietly = 0;
  149.  
  150. /* Avoid using X no matter what else. (-x --no-x) */
  151. int no_x = 0;
  152.  
  153. /* What kind of display? */
  154. int using_x = 0;
  155. int using_curses = 0;
  156.  
  157.  
  158. /* Cell size paramaters. */
  159. unsigned int default_width = 8;
  160. unsigned int default_height = 1;
  161.  
  162. /* These values are used by clear_spreadsheet ()
  163.  * to restore the defaults.
  164.  */
  165. unsigned int saved_default_width = 8;
  166. unsigned int saved_default_height = 1;
  167.  
  168. /* Other cell defaults: */
  169. int default_jst = JST_LFT;
  170. int default_fmt = FMT_GEN;
  171. int default_lock = LCK_UNL;
  172.  
  173. /* Pointers to interesting cmd_func structures. */
  174. struct cmd_func *end_macro_cmd;
  175. struct cmd_func *digit_0_cmd;
  176. struct cmd_func *digit_9_cmd;
  177. struct cmd_func * break_cmd;
  178. struct cmd_func * universal_arg_cmd;
  179.  
  180. /* A bland signal handler. */
  181. #ifdef __STDC__
  182. static RETSIGTYPE
  183. got_sig (int sig)
  184. #else
  185. static RETSIGTYPE
  186. got_sig (sig)
  187.      int sig;
  188. #endif
  189. {
  190. }
  191.  
  192.  
  193.  
  194. /* An parser for the language grokked by option setting commands. */
  195.  
  196. #ifdef __STDC__
  197. static int 
  198. do_set_option (char *ptr)
  199. #else
  200. static int 
  201. do_set_option (ptr)
  202.      char *ptr;
  203. #endif
  204. {
  205.   int set_opt = 1;
  206.  
  207.   while (*ptr == ' ')
  208.     ptr++;
  209.   if (!strincmp ("no", ptr, 2))
  210.     {
  211.       ptr += 2;
  212.       set_opt = 0;
  213.       while (*ptr == ' ')
  214.     ptr++;
  215.     }
  216.   if (!stricmp ("auto", ptr))
  217.     {
  218.       auto_recalc = set_opt;
  219.       return 0;
  220.     }
  221.   if (!stricmp ("bkgrnd", ptr) || !stricmp ("background", ptr))
  222.     {
  223.       bkgrnd_recalc = set_opt;
  224.       return 0;
  225.     }
  226.   if (!stricmp ("a0", ptr))
  227.     {
  228.       a0 = set_opt;
  229.       io_repaint ();
  230.       return 0;
  231.     }
  232.   if (!stricmp ("backup", ptr))
  233.     {
  234.       __make_backups = set_opt;
  235.       return 0;
  236.     }
  237.   if (!stricmp ("bkup_copy", ptr))
  238.     {
  239.       __backup_by_copying = set_opt;
  240.       return 0;
  241.     }
  242.   if (set_opt && !strincmp ("ticks ", ptr, 6))
  243.     {
  244.       ptr += 6;
  245.       cell_timer_seconds = astol (&ptr);
  246.       return 0;
  247.     }
  248.   if (set_opt && !strincmp ("print ", ptr, 6))
  249.     {
  250.       ptr += 6;
  251.       print_width = astol (&ptr);
  252.       return 0;
  253.     }
  254.   if (set_opt && !strincmp ("file ", ptr, 5))
  255.     {
  256. #ifdef USE_DLD
  257.       char *tmpstr;
  258.  
  259.       ptr += 5;
  260.       tmpstr = ck_malloc (strlen (ptr) + 20);
  261.       if (io_name)
  262.     {
  263.       sprintf (tmpstr, "%s.o", ptr);
  264.       if (dld_unlink_by_file (tmpstr, 0))
  265.         {
  266.           io_error_msg ("Couldn't unlink old file format %s: %s", io_name, (dld_errno < 0 || dld_errno > dld_nerr) ? "Unknown error" : dld_errlst[dld_errno]);
  267.           goto bad_file;
  268.         }
  269.       free (io_name);
  270.     }
  271.       if (!stricmp (ptr, "panic"))
  272.     {
  273.       io_name = 0;
  274.       read_file = panic_read_file;
  275.       write_file = panic_write_file;
  276.       set_file_opts = panic_set_options;
  277.       show_file_opts = panic_show_options;
  278.       free (tmpstr);
  279.       return 0;
  280.     }
  281.       io_name = strdup (ptr);
  282.       sprintf (tmpstr, "%s.o", ptr);
  283.       if (dld_link (tmpstr))
  284.     {
  285.       io_error_msg ("Couldn't link new file format %s: %s", io_name, (dld_errno < 0 || dld_errno > dld_nerr) ? "Unknown error" : dld_errlst[dld_errno]);
  286.       goto bad_file;
  287.     }
  288.       if (dld_link ("libc.a"))
  289.     io_error_msg ("Couldn't link libc.a");
  290.       if (dld_link ("libm.a"))
  291.     io_error_msg ("Couldn't link libm.a");
  292.  
  293.       sprintf (tmpstr, "%s_read_file", ptr);
  294.       read_file = dld_function_executable_p (tmpstr) ? dld_get_func (tmpstr) : 0;
  295.       sprintf (tmpstr, "%s_write_file", ptr);
  296.       write_file = dld_function_executable_p (tmpstr) ? dld_get_func (tmpstr) : 0;
  297.  
  298.       sprintf (tmpstr, "%s_set_options", ptr);
  299.       set_file_opts = (int (*)()) (dld_function_executable_p (tmpstr) ? dld_get_func (tmpstr) : 0);
  300.       sprintf (tmpstr, "%s_show_options", ptr);
  301.       show_file_opts = dld_function_executable_p (tmpstr) ? dld_get_func (tmpstr) : 0;
  302.  
  303.       if (!read_file
  304.       || !write_file
  305.       || !set_file_opts
  306.       || !show_file_opts)
  307.     {
  308.       char **missing;
  309.       int n;
  310.  
  311.       missing = dld_list_undefined_sym ();
  312.       io_text_start ();
  313.       io_text_line ("Undefined symbols in file format %s:", ptr);
  314.       io_text_line ("");
  315.       for (n = 0; n < dld_undefined_sym_count; n++)
  316.         io_text_line ("%s", missing[n]);
  317.       io_text_line ("");
  318.       io_text_finish ();
  319.       free (missing);
  320.       io_error_msg ("File format %s has undefined symbols: not loaded", ptr);
  321.     bad_file:
  322.       sprintf (tmpstr, "%s.o", io_name);
  323.       dld_unlink_by_file (io_name, 0);
  324.       if (io_name)
  325.         free (io_name);
  326.       io_name = 0;
  327.       read_file = panic_read_file;
  328.       write_file = panic_write_file;
  329.       set_file_opts = panic_set_options;
  330.       show_file_opts = panic_show_options;
  331.     }
  332.       free (tmpstr);
  333. #else
  334.       ptr += 5;
  335.       if (!stricmp ("oleo", ptr))
  336.     {
  337.       read_file = oleo_read_file;
  338.       write_file = oleo_write_file;
  339.       set_file_opts = oleo_set_options;
  340.       show_file_opts = oleo_show_options;
  341.     }
  342.       else if (!stricmp ("sylk", ptr))
  343.     {
  344.       sylk_a0 = 1;
  345.       read_file = sylk_read_file;
  346.       write_file = sylk_write_file;
  347.       set_file_opts = sylk_set_options;
  348.       show_file_opts = sylk_show_options;
  349.     }
  350.       else if (!stricmp ("sylk-noa0", ptr))
  351.     {
  352.       sylk_a0 = 0;
  353.       read_file = sylk_read_file;
  354.       write_file = sylk_write_file;
  355.       set_file_opts = sylk_set_options;
  356.       show_file_opts = sylk_show_options;
  357.     }
  358.       else if (!stricmp ("sc", ptr))
  359.     {
  360.       read_file = sc_read_file;
  361.       write_file = sc_write_file;
  362.       set_file_opts = sc_set_options;
  363.       show_file_opts = sc_show_options;
  364.     }
  365.       else if (!stricmp ("panic", ptr))
  366.     {
  367.       read_file = panic_read_file;
  368.       write_file = panic_write_file;
  369.       set_file_opts = panic_set_options;
  370.       show_file_opts = panic_show_options;
  371.     }
  372.       else if (!stricmp ("list", ptr))
  373.     {
  374.       read_file = list_read_file;
  375.       write_file = list_write_file;
  376.       set_file_opts = list_set_options;
  377.       show_file_opts = list_show_options;
  378.       /*if (ptr[4])
  379.         {
  380.         ptr+=4;
  381.         sl_sep=string_to_char(&ptr);
  382.         } */
  383.     }
  384.       else
  385.     io_error_msg ("Unknown file format %s", ptr);
  386. #endif
  387.       return 0;
  388.     }
  389. #ifdef USE_DLD
  390.   else if (!strincmp (ptr, "load ", 5))
  391.     {
  392.       char *tmpstr;
  393.       struct function *new_funs;
  394.       struct cmd_func *new_cmds;
  395.       struct keymap **new_maps;
  396.       void (*init_cmd) ();
  397.  
  398.       ptr += 5;
  399.       tmpstr = ck_malloc (strlen (ptr) + 20);
  400.       sprintf (tmpstr, "%s.o", ptr);
  401.       if (dld_link (tmpstr))
  402.     {
  403.       io_error_msg ("Couldn't link %s: %s", tmpstr, (dld_errno < 0 || dld_errno > dld_nerr) ? "Unknown error" : dld_errlst[dld_errno]);
  404.       free (tmpstr);
  405.       return 0;
  406.     }
  407.       if (dld_link ("libc.a"))
  408.     io_error_msg ("Couldn't link libc.a");
  409.       if (dld_link ("libm.a"))
  410.     io_error_msg ("Couldn't link libm.a");
  411.  
  412.       if (dld_undefined_sym_count)
  413.     {
  414.       char **missing;
  415.       int n;
  416.  
  417.       missing = dld_list_undefined_sym ();
  418.       io_text_start ();
  419.       io_text_line ("Undefined symbols in file format %s:", ptr);
  420.       io_text_line ("");
  421.       for (n = 0; n < dld_undefined_sym_count; n++)
  422.         io_text_line ("%s", missing[n]);
  423.       io_text_line ("");
  424.       io_text_finish ();
  425.       free (missing);
  426.       io_error_msg ("%d undefined symbols in %s", dld_undefined_sym_count, ptr);
  427.       dld_unlink_by_file (tmpstr, 0);
  428.       free (tmpstr);
  429.       return 0;
  430.     }
  431.       sprintf (tmpstr, "%s_funs", ptr);
  432.       new_funs = (struct function *) dld_get_symbol (tmpstr);
  433.       if (new_funs)
  434.     add_usr_funs (new_funs);
  435.       sprintf (tmpstr, "%s_cmds", ptr);
  436.       new_cmds = (struct cmd_func *) dld_get_symbol (tmpstr);
  437.       if (new_cmds)
  438.     add_usr_cmds (new_cmds);
  439.       sprintf (tmpstr, "%s_maps", ptr);
  440.       new_maps = (struct keymap **) dld_get_symbol (tmpstr);
  441.       if (new_maps)
  442.     add_usr_maps (new_maps);
  443.       if (!new_funs && !new_cmds && !new_maps)
  444.     {
  445.       io_error_msg ("Couldn't find anything to load in %s", ptr);
  446.       sprintf (tmpstr, "%s.o", ptr);
  447.       dld_unlink_by_file (tmpstr, 0);
  448.     }
  449.       sprintf (tmpstr, "%s_init", ptr);
  450.       init_cmd = dld_function_executable_p (tmpstr) ? dld_get_func (tmpstr) : 0;
  451.       if (init_cmd)
  452.     (*init_cmd) ();
  453.       free (tmpstr);
  454.       return 0;
  455.     }
  456. #endif
  457.   if (set_window_option (set_opt, ptr) == 0)
  458.     {
  459.       if ((*set_file_opts) (set_opt, ptr))
  460.     io_error_msg ("Unknown option '%s'", ptr);
  461.       return 0;
  462.     }
  463.   return 1;
  464. }
  465.  
  466. #ifdef __STDC__
  467. void
  468. set_options (char * ptr)
  469. #else
  470. void
  471. set_options (ptr)
  472.      char *ptr;
  473. #endif
  474. {
  475.   if (do_set_option (ptr))
  476.     io_recenter_cur_win ();
  477. }
  478.  
  479. #ifdef __STDC__
  480. void 
  481. show_options (void)
  482. #else
  483. void 
  484. show_options ()
  485. #endif
  486. {
  487.   int n;
  488.   int fmts;
  489.   char *data_buf[9];
  490.  
  491.   n = auto_recalc;
  492.   io_text_start ();
  493.  
  494.   io_text_line ("auto-recalculation: %s        Recalculate in background: %s",
  495.         n ? " on" : "off", bkgrnd_recalc ? "on" : "off");
  496.   io_text_line ("make backup files:  %s        Copy files into backups:   %s",
  497.     __make_backups ? " on" : "off", __backup_by_copying ? "on" : "off");
  498.  
  499.   io_text_line ("Asynchronous updates every %u ???",
  500.         cell_timer_seconds);
  501.  
  502.   io_text_line ("Print width:      %5u", print_width);
  503.  
  504.   io_text_line ("");
  505.  
  506.   (*show_file_opts) ();
  507.  
  508.   io_text_line ("");
  509.   show_window_options ();
  510.   io_text_line ("");
  511.  
  512.   fmts = usr_set_fmts ();
  513.   if (fmts)
  514.     {
  515.       io_text_line ("User-defined formats:");
  516.       io_text_line ("Fmt    +Hdr    -Hdr   +Trlr   -Trlr    Zero   Comma Decimal  Prec         Scale");
  517.       for (n = 0; n < 16; n++)
  518.     {
  519.       if (fmts & (1 << n))
  520.         {
  521.           get_usr_stats (n, data_buf);
  522.           io_text_line ("%3d %7s %7s %7s %7s %7s %7s %7s %5s %13s",
  523.                 n + 1,
  524.                 data_buf[0],
  525.                 data_buf[1],
  526.                 data_buf[2],
  527.                 data_buf[3],
  528.                 data_buf[4],
  529.                 data_buf[5],
  530.                 data_buf[6],
  531.                 data_buf[7],
  532.                 data_buf[8]);
  533.         }
  534.     }
  535.     }
  536.   else
  537.     io_text_line ("No user-defined formats have been defined");
  538.  
  539.   io_text_finish ();
  540. }
  541.  
  542.  
  543. #ifdef __STDC__
  544. void
  545. read_mp_usr_fmt (char *ptr)
  546. #else
  547. void
  548. read_mp_usr_fmt (ptr)
  549.      char *ptr;
  550. #endif
  551. {
  552.   int usr_n = -1;
  553.   int n_chrs = 0;
  554.   char *p;
  555.   char *buf[9];
  556.   int i;
  557.  
  558.   for (i = 0; i < 9; i++)
  559.     buf[i] = "";
  560.   p = ptr;
  561.   while (*p == ';')
  562.     {
  563.       *p++ = '\0';
  564.       switch (*p++)
  565.     {
  566.     case 'N':
  567.       usr_n = astol (&p) - 1;
  568.       break;
  569.     case 'H':
  570.       switch (*p++)
  571.         {
  572.         case 'P':
  573.           i = 0;
  574.           break;
  575.         case 'N':
  576.           i = 1;
  577.           break;
  578.         default:
  579.           goto badline;
  580.         }
  581.       goto count_chars;
  582.     case 'T':
  583.       switch (*p++)
  584.         {
  585.         case 'P':
  586.           i = 2;
  587.           break;
  588.         case 'N':
  589.           i = 3;
  590.           break;
  591.         default:
  592.           goto badline;
  593.         }
  594.       goto count_chars;
  595.  
  596.     case 'Z':
  597.       i = 4;
  598.       goto count_chars;
  599.  
  600.     case 'C':
  601.       i = 5;
  602.       goto count_chars;
  603.  
  604.     case 'D':
  605.       i = 6;
  606.       goto count_chars;
  607.  
  608.     case 'P':
  609.       i = 7;
  610.       goto count_chars;
  611.  
  612.     case 'S':
  613.       i = 8;
  614.       goto count_chars;
  615.  
  616.     count_chars:
  617.       buf[i] = p;
  618.       n_chrs++;
  619.       while (*p && *p != ';')
  620.         {
  621.           p++;
  622.           n_chrs++;
  623.         }
  624.       break;
  625.  
  626.     default:
  627.     badline:
  628.       io_error_msg ("Unknown OLEO line %s", ptr);
  629.       return;
  630.     }
  631.     }
  632.   if (*p || usr_n < 0 || usr_n > 15)
  633.     goto badline;
  634.  
  635.   set_usr_stats (usr_n, buf);
  636. }
  637.  
  638. /* Modify this to write out *all* the options */
  639. #ifdef __STDC__
  640. void
  641. write_mp_options (FILE *fp)
  642. #else
  643. void
  644. write_mp_options (fp)
  645.      FILE *fp;
  646. #endif
  647. {
  648.   fprintf (fp, "O;%sauto;%sbackground;%sa0;ticks %d\n",
  649.        auto_recalc ? "" : "no",
  650.        bkgrnd_recalc ? "" : "no",
  651.        a0 ? "" : "no",
  652.        cell_timer_seconds);
  653. }
  654.  
  655. #ifdef __STDC__
  656. void 
  657. read_mp_options (char *str)
  658. #else
  659. void 
  660. read_mp_options (str)
  661.      char *str;
  662. #endif
  663. {
  664.   char *np;
  665.  
  666.   while (np = (char *)index (str, ';'))
  667.     {
  668.       *np = '\0';
  669.       do_set_option (str);
  670.       *np++ = ';';
  671.       str = np;
  672.     }
  673.   if (np = (char *)rindex (str, '\n'))
  674.     *np = '\0';
  675.   (void) do_set_option (str);
  676. }
  677.  
  678.  
  679.  
  680.  
  681. /* Commands related to variables. */
  682.  
  683. #ifdef __STDC__
  684. void
  685. set_var (char * var, char * val)
  686. #else
  687. void
  688. set_var (var, val)
  689.      char * var;
  690.      char * val;
  691. #endif
  692. {
  693.   char *ret;
  694.   if (val)
  695.     {
  696.       while (isspace (*val))
  697.     ++val;
  698.       if (!*val)
  699.     val = 0;
  700.     }
  701.   modified = 1;
  702.   ret = new_var_value (var, strlen(var), val);
  703.   if (ret)
  704.     io_error_msg ("Can't set-variable %s: %s\n", var, ret);
  705. }
  706.  
  707. #ifdef __STDC__
  708. void
  709. show_var (char *ptr)
  710. #else
  711. void
  712. show_var (ptr)
  713.      char *ptr;
  714. #endif
  715. {
  716.   struct var *v;
  717.   int num;
  718.  
  719.   while (*ptr == ' ')
  720.     ptr++;
  721.   for (num = 0; ptr[num] && ptr[num] != ' '; num++)
  722.     ;
  723.  
  724.   v = find_var (ptr, num);
  725.   if (!v || v->var_flags == VAR_UNDEF)
  726.     {
  727.       io_error_msg ("There is no '%s'", ptr);
  728.       return;
  729.     }
  730.   if (a0)
  731.     {
  732.       if (v->v_rng.lr != v->v_rng.hr || v->v_rng.lc != v->v_rng.hc)
  733.     /* FOO */ sprintf (print_buf, "%s $%s$%u:$%s$%u", v->var_name, col_to_str (v->v_rng.lc), v->v_rng.lr, col_to_str (v->v_rng.hc), v->v_rng.hr);
  734.       else
  735.     /* FOO */ sprintf (print_buf, "%s $%s$%u", v->var_name, col_to_str (v->v_rng.lc), v->v_rng.lr);
  736.     }
  737.   else
  738.     sprintf (print_buf, "%s %s", v->var_name, range_name (&(v->v_rng)));
  739.   io_info_msg (print_buf);
  740. }
  741.  
  742. #ifdef __STDC__
  743. static void
  744. show_a_var (char *name, struct var *v)
  745. #else
  746. static void
  747. show_a_var (name, v)
  748.      char *name;
  749.      struct var *v;
  750. #endif
  751. {
  752.   if (v->var_flags == VAR_UNDEF)
  753.     return;
  754.   if (a0)
  755.     {
  756.       if (v->v_rng.lr != v->v_rng.hr || v->v_rng.lc != v->v_rng.hc)
  757.     /* FOO */ io_text_line ("%-20s  $%s$%u:$%s$%u", v->var_name, col_to_str (v->v_rng.lc), v->v_rng.lr, col_to_str (v->v_rng.hc), v->v_rng.hr);
  758.       else
  759.     /* FOO */ io_text_line ("%-20s  $%s$%u", v->var_name, col_to_str (v->v_rng.lc), v->v_rng.lr);
  760.     }
  761.   else
  762.     io_text_line ("%-20s  %s", v->var_name, range_name (&(v->v_rng)));
  763. }
  764.  
  765. #ifdef __STDC__
  766. void
  767. show_all_var (void)
  768. #else
  769. void
  770. show_all_var ()
  771. #endif
  772. {
  773.   io_text_start ();
  774.   io_text_line ("%-20s  Current Value", "Variable Name");
  775.   for_all_vars (show_a_var);
  776.   io_text_finish ();
  777. }
  778.  
  779. static FILE * write_variable_fp = 0;
  780.  
  781. #ifdef __STDC__
  782. static void
  783. write_a_var (char *name, struct var *v)
  784. #else
  785. static void
  786. write_a_var (name, v)
  787.      char *name;
  788.      struct var *v;
  789. #endif
  790. {
  791.   CELLREF r, c;
  792.   if (v->var_flags == VAR_UNDEF)
  793.     return;
  794.   r = v->v_rng.lr;
  795.   c = v->v_rng.lc;
  796.   if (v->var_flags == VAR_CELL)
  797.     fprintf (write_variable_fp, "%s=%s\n",
  798.          v->var_name, cell_value_string (r, c));
  799. }
  800.  
  801. #ifdef __STDC__
  802. void
  803. write_variables (FILE * fp)
  804. #else
  805. void
  806. write_variables (fp)
  807.      FILE * fp;
  808. #endif
  809. {
  810.   if (write_variable_fp)
  811.     io_error_msg ("Can't re-enter write_variables.");
  812.   else
  813.     {
  814.       write_variable_fp = fp;
  815.       for_all_vars (write_a_var);
  816.       write_variable_fp = 0;
  817.     }
  818. }
  819.  
  820. #ifdef __STDC__
  821. void
  822. read_variables (FILE * fp)
  823. #else
  824. void
  825. read_variables (fp)
  826.      FILE * fp;
  827. #endif
  828. {
  829.   char buf[1024];
  830.   int lineno = 0;
  831.   while (fgets (buf, 1024, fp))
  832.     {
  833.       char * ptr;
  834.       for (ptr = buf; *ptr && *ptr != '\n'; ++ptr)
  835.     ;
  836.       *ptr = '\0';
  837.       for (ptr = buf; isspace (*ptr); ptr++)
  838.     ;
  839.       if (!*ptr || (*ptr == '#'))
  840.     continue;
  841.       {
  842.     char * var_name = ptr;
  843.     int var_name_len;
  844.     char * value_string;
  845.     while (*ptr && *ptr != '=')
  846.       ++ptr;
  847.     if (!*ptr)
  848.       {
  849.         io_error_msg ("read-variables: format error near line %d.", lineno);
  850.         return;
  851.       }
  852.     var_name_len = ptr - var_name;
  853.     ++ptr;
  854.     value_string = ptr;
  855.     {
  856.       struct var * var = find_var (var_name, var_name_len);
  857.       if (var)
  858.         {
  859.           switch (var->var_flags)
  860.         {
  861.         case VAR_UNDEF:
  862.           break;
  863.         case VAR_CELL:
  864.           {
  865.             char * error = new_value (var->v_rng.lr, var->v_rng.lc,
  866.                           value_string); 
  867.             if (error)
  868.               {
  869.             io_error_msg (error);
  870.             return;    /* actually, io_error_msg never returns. */
  871.               }
  872.             else
  873.               modified = 1;
  874.             break;
  875.           }
  876.         case VAR_RANGE:
  877.           io_error_msg ("read-variables (line %d): ranges not supported.",
  878.                 lineno);
  879.           return;
  880.         }
  881.         }
  882.     }
  883.       }
  884.       ++lineno;
  885.     }
  886.   if (!feof (fp))
  887.     {
  888.       io_error_msg ("read-variables: read error near line %d.", lineno);
  889.       return;
  890.     }
  891. }
  892.  
  893.  
  894.  
  895. #ifdef __STDC__
  896. void
  897. init_maps (void)
  898. #else
  899. void
  900. init_maps ()
  901. #endif
  902. {
  903.   num_maps = 0;
  904.   the_maps = 0;
  905.   map_names = 0;
  906.   map_prompts = 0;
  907.  
  908.   the_funcs = ck_malloc (sizeof (struct cmd_func *) * 2);
  909.   num_funcs = 1;
  910.   the_funcs[0] = &cmd_funcs[0];
  911.  
  912.   find_func (0, &end_macro_cmd, "end-macro");
  913.   find_func (0, &digit_0_cmd, "digit-0");
  914.   find_func (0, &digit_9_cmd, "digit-9");
  915.   find_func (0, &break_cmd, "break");
  916.   find_func (0, &universal_arg_cmd, "universal-argument");
  917.  
  918.   create_keymap ("universal", 0);
  919.   push_command_frame (0, 0, 0);
  920. }
  921.  
  922. #ifdef __STDC__
  923. int 
  924. add_usr_cmds (struct cmd_func *new_cmds)
  925. #else
  926. int 
  927. add_usr_cmds (new_cmds)
  928.      struct cmd_func *new_cmds;
  929. #endif
  930. {
  931.   num_funcs++;
  932.   the_funcs = ck_realloc (the_funcs, num_funcs * sizeof (struct cmd_func *));
  933.   the_funcs[num_funcs - 1] = new_cmds;
  934.   return num_funcs - 1;
  935. }
  936.  
  937. #ifdef USE_DLD
  938. #ifdef __STDC__
  939. static int 
  940. add_usr_maps (struct keymap **new_maps)
  941. #else
  942. static int 
  943. add_usr_maps (new_maps)
  944.      struct keymap **new_maps;
  945. #endif
  946. {
  947.   int n;
  948.  
  949.   for (n = 1; new_maps[n]; n++)
  950.     ;
  951.   the_maps = ck_realloc (the_maps, (n + num_maps) * sizeof (struct keymap *));
  952.   bcopy (new_maps, &the_maps[num_maps], n * sizeof (struct keymap *));
  953.   num_maps += n;
  954.   return num_maps - n;
  955. }
  956. #endif /* USE_DLD */
  957.  
  958. #ifdef __STDC__
  959. static void
  960. show_usage (void)
  961. #else
  962. static void
  963. show_usage ()
  964. #endif
  965. {
  966.   char ** use = usage;
  967.   fprintf (stderr, "Usage: %s ", argv_name);
  968.   while (*use)
  969.     {
  970.       fprintf (stderr, "%s\n", *use);
  971.       ++use;
  972.     }
  973. }
  974.  
  975. #ifdef __STDC__
  976. static RETSIGTYPE
  977. continue_oleo (int sig)
  978. #else
  979. static RETSIGTYPE
  980. continue_oleo (sig)
  981.      int sig;
  982. #endif
  983. {
  984.   io_repaint ();
  985.   if (using_curses)
  986.     cont_curses ();
  987. }
  988.  
  989. int display_opened = 0;
  990.  
  991. extern int sneaky_linec;
  992.  
  993. #ifdef __STDC__
  994. int 
  995. main (int argc, char **argv)
  996. #else
  997. int 
  998. main (argc, argv)
  999.      int argc;
  1000.      char **argv;
  1001. #endif
  1002. {
  1003.   volatile int ignore_init_file = 0;
  1004.   FILE * init_fp[2];
  1005.   char * init_file_names[2];
  1006.   volatile int init_fpc = 0;
  1007.   int command_line_file = 0;    /* was there one? */
  1008.  
  1009.   argv_name = argv[0];
  1010.   __make_backups = 1;
  1011.  
  1012.   /* Set up the minimal io handler. */
  1013. #if 0
  1014.   cmd_graphics ();
  1015. #endif
  1016.  
  1017.   {
  1018.     int opt;
  1019.     for (opt = getopt_long (argc, argv, short_options, long_options, (int *)0);
  1020.      opt != EOF;
  1021.      opt = getopt_long (argc, argv, short_options, long_options, (int *)0))
  1022.       {
  1023.     switch (opt)
  1024.       {
  1025.       case 'V':
  1026.         fprintf  (stdout, "%s\n", oleo_version_string);
  1027.         exit (0);
  1028.         break;
  1029.       case 'q':
  1030.         spread_quietly = 1;
  1031.         break;
  1032.       case 'f':
  1033.         ignore_init_file = 1;
  1034.         break;
  1035.       case 'x':
  1036.         no_x = 1;
  1037.         break;
  1038.       case 'h':
  1039.         show_usage ();
  1040.         exit (0);
  1041.         break;
  1042.       }
  1043.       }
  1044.   }
  1045.   if (argc - optind > 1)
  1046.     {
  1047.       show_usage ();
  1048.       exit (1);
  1049.     }
  1050.   init_infinity ();
  1051.   init_mem ();
  1052.   init_eval ();
  1053.   init_refs ();
  1054.   init_cells ();
  1055.   init_fonts ();
  1056.   init_info ();
  1057.  
  1058. #ifdef USE_DLD
  1059.   if (!index (argv_name, '/'))
  1060.     {
  1061.       char *name;
  1062.  
  1063.       name = dld_find_executable (argv_name);
  1064.       num = dld_init (name);
  1065.       free (name);
  1066.     }
  1067.   else
  1068.     num = dld_init (argv_name);
  1069.   if (num)
  1070.     io_error_msg ("dld_init() failed: %s", (dld_errno < 0 || dld_errno > dld_nerr) ? "Unknown error" : dld_errlst[dld_errno]);
  1071.   dld_search_path = ":/usr/local/lib/oleo:/lib:/usr/lib:/usr/local/lib";
  1072. #endif
  1073.  
  1074.  
  1075.  
  1076.   /* Find the init files. 
  1077.    * This is done even if ignore_init_file is true because
  1078.    * it effects whether the disclaimer will be shown.
  1079.    */
  1080.     {
  1081.       char *ptr, *home;
  1082.       
  1083.       home = getenv ("HOME");
  1084.       if (home)
  1085.     {
  1086.       ptr = mk_sprintf ("%s/%s", home, RCFILE);
  1087.       init_fp[init_fpc] = fopen (ptr, "r");
  1088.       init_file_names[init_fpc] = ptr;
  1089.       if (init_fp[init_fpc])
  1090.         ++init_fpc;
  1091.     }
  1092.       
  1093.       init_fp[init_fpc] = fopen (RCFILE, "r");
  1094.       if (init_fp[init_fpc])
  1095.     ++init_fpc;
  1096.     }
  1097.  
  1098.   FD_ZERO (&read_fd_set);
  1099.   FD_ZERO (&read_pending_fd_set);
  1100.   FD_ZERO (&exception_fd_set);
  1101.   FD_ZERO (&exception_pending_fd_set);
  1102.  
  1103. #ifdef HAVE_X11_X_H
  1104.   if (!no_x)
  1105.     get_x11_args (&argc, argv);
  1106.   if (!no_x && io_x11_display_name)
  1107.     {
  1108.       x11_graphics ();
  1109.       using_x = 1;
  1110.     }
  1111.   else
  1112. #endif
  1113.     {
  1114.       tty_graphics ();
  1115.       using_curses = 1;
  1116.       /* Allow the disclaimer to be read. */
  1117.       if (!init_fpc && !spread_quietly)
  1118.     sleep (5);
  1119.     }
  1120.  
  1121.   io_open_display ();
  1122.  
  1123.   init_graphing ();
  1124.  
  1125.   if (setjmp (error_exception))
  1126.     {
  1127.       fprintf (stderr, "Error in the builtin init scripts (a bug!).");
  1128.       exit (69);
  1129.     }
  1130.   else
  1131.     {
  1132.       init_maps ();
  1133.       init_named_macro_strings ();
  1134.       run_init_cmds ();
  1135.     }
  1136.  
  1137.   /* These probably don't all need to be ifdef, but
  1138.    * it is harmless.
  1139.    */
  1140. #ifdef SIGCONT
  1141.   signal (SIGCONT, continue_oleo);
  1142. #endif
  1143. #ifdef SIGINT
  1144.   signal (SIGINT, got_sig);
  1145. #endif
  1146. #ifdef SIGQUIT
  1147.   signal (SIGQUIT, got_sig);
  1148. #endif
  1149. #ifdef SIGILL
  1150.   signal (SIGILL, got_sig);
  1151. #endif
  1152. #ifdef SIGEMT
  1153.   signal (SIGEMT, got_sig);
  1154. #endif
  1155. #ifdef SIGBUS
  1156.   signal (SIGBUS, got_sig);
  1157. #endif
  1158. #ifdef SIGSEGV
  1159.   signal (SIGSEGV, got_sig);
  1160. #endif
  1161. #ifdef SIGPIPE
  1162.   signal (SIGPIPE, got_sig);
  1163. #endif
  1164.  
  1165.   /* Read the init file. */
  1166.   {
  1167.     volatile int x;
  1168.     for (x = 0; x < init_fpc; ++x)
  1169.       {
  1170.     if (setjmp (error_exception))
  1171.       {
  1172.         fprintf (stderr, "   error occured in init file %s near line %d.",
  1173.              init_file_names [x], sneaky_linec);
  1174.       }
  1175.     else
  1176.       if (!ignore_init_file)
  1177.         read_cmds_cmd (init_fp[x]);
  1178.     fclose (init_fp[x]);
  1179.       }
  1180.   }
  1181.  
  1182.  
  1183.   if (argc - optind == 1)
  1184.     {
  1185.       FILE * fp;
  1186.       /* fixme: record file name */
  1187.       ++optind;
  1188.       if (fp = fopen (argv[1], "r"))
  1189.     {
  1190.       if (setjmp (error_exception))
  1191.         fprintf (stderr, "  error occured reading %s", argv[1]);
  1192.       else
  1193.         read_file_and_run_hooks (fp, 0, argv[1]);
  1194.       fclose (fp);
  1195.       command_line_file = 1;
  1196.     }
  1197.       else
  1198.     fprintf (stderr, "Can't open %s: %s", argv[1], err_msg ());
  1199.     }
  1200.   /* Force the command frame to be rebuilt now that the keymaps exist. */
  1201.   {
  1202.     struct command_frame * last_of_the_old = the_cmd_frame->next;
  1203.     while (the_cmd_frame != last_of_the_old)
  1204.       free_cmd_frame (the_cmd_frame);
  1205.     free_cmd_frame (last_of_the_old);
  1206.   }
  1207.   io_recenter_cur_win ();
  1208.  
  1209.   display_opened = 1;
  1210.  
  1211.   if (!command_line_file)
  1212.     run_string_as_macro
  1213.       ("{pushback-keystroke}{builtin-help _NON_WARRANTY_}");
  1214.   while (1)
  1215.     {
  1216.       setjmp (error_exception);
  1217.       command_loop (0);
  1218.     }
  1219. }
  1220.  
  1221.  
  1222.